package com.paul.service;

import java.util.ArrayList;

/**
 * @author Paul
 * @version 1.0
 *
 */

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Qualifier;
import javax.servlet.ServletContext;

import org.apache.ibatis.annotations.Update;

import com.domain.User;
import com.paul.dao.UserDao;
import com.paul.dao.UserDaoImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;
@Service("userService")
public class UserServiceImpl implements UserService {

	//public UserDao userDao = new UserDaoImpl();
	@Autowired
	public UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}
	@Autowired(required = false) 
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	// 验证用户
	public boolean checkUser(String username, String password) {

		UserDao userDao = getUserDao();

		boolean flag = false;
		String result = userDao.check(username);

		if (result.equals(password)) {
			flag = true;
		}
		System.out.println("删除多条记录后的flag是: "+flag);
		return flag;

	}

	// 删除用户
	public boolean deleteUser(String userIds) {
		//处理要删除的用户列表
		boolean flag = true;
		boolean ff=true;
		System.out.println("dededede的删除用户信息列表是" + userIds);	
		String[] array = userIds.split(",");
		//List<String> userIdList = new ArrayList<String>();
		for (String userId : array)
		{
			UserDao userDao = getUserDao();	
			int result = userDao.delete(userId);
			if (result == 1) {
				ff = true;
			}else {
				ff = false;
			}
			System.out.println("删除的flag是: "+ff);
			flag=flag&&ff;
		}
		
		return flag;
	}

	// 编辑用户
	public boolean editUser(User user) {
		UserDao userDao = getUserDao();
		boolean flag = false;
		
		int result = userDao.update(user);

		if (result == 1) {
			flag = true;
		}

		return flag;
	}

	// 新增用户
	public boolean insertUser(User user) {
		UserDao userDao = getUserDao();
		boolean flag = false;

		/*
		 * Map<String, String> params = new HashMap<String, String>(); boolean
		 * flag = false; params.put("username", username);
		 * params.put("password", password); params.put("email", email);
		 * params.put("grade", grade);
		 */

		int result = userDao.insert(user);
		System.out.println(result);

		if (result == 1) {
			flag = true;
		}
		return flag;
	}

	// 欢迎界面显示数据
	public Map<String, Object> showUser(String username, String email, String page, String rows) {
		UserDao userDao = getUserDao();
		Map<String, Object> bMap = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();

		int pageNow = Integer.parseInt(page);
		int pageSize = Integer.parseInt(rows);
		int pageCount = pageSize * (pageNow - 1);

		params.put("username", username);
		params.put("email", email);
		params.put("pageCount", pageCount);
		params.put("pageSize", pageSize);

		try {
			bMap = userDao.search(params);

		} catch (Exception ex) {
			System.out.println("这里报异常了");
		}
		return bMap;
	}
}
